package com.zenithsun.common.mytest.controller;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.syzg.common.config.SysProperties;
import com.syzg.common.controller.BaseController;
import com.syzg.common.model.WebInfo;
import com.syzg.common.util.SearchMap;
import com.syzg.common.util.MethodUtil;
import com.syzg.common.model.WebObj;
import com.syzg.common.util.PKCreator;
import com.syzg.common.exception.BaseException;
import com.syzg.core.page.PageResult;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zenithsun.common.mytest.model.Mytest;
import com.zenithsun.common.mytest.service.MytestService;

@Controller
@RequestMapping("/path/mytest")
public class MytestController extends BaseController{
	
	static Logger log = LoggerFactory.getLogger(MytestController.class);
	
	@Resource
	private MytestService mytestService;
	
	@RequestMapping("/listMytest")
	public ModelAndView listMytest(WebObj webObj)throws Exception{
		ModelAndView mv = new ModelAndView("/base/jsp/mytest/listMytest.jsp");
		SearchMap searchMap = SearchMap.forClass(Mytest.class);
		PageResult pageResult = mytestService.findObjectsForPage(webObj.getPageNo(), SysProperties.PAGESIZE, searchMap);
        webObj.setPageResult(pageResult);
        mv.addObject("webObj",webObj);
        return mv;
	}
	
	@RequestMapping("/initMytest")
	public ModelAndView initMytest() throws Exception {
		ModelAndView mv = new ModelAndView("/base/jsp/mytest/addMytest.jsp");
		try {
			mv.addObject("mytest", new Mytest());
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.mytest.setCreatePersonId(new String(loginObj.getPersonId()));
			this.mytest.setCreator(loginObj.getPersonName());
			this.mytest.setCreateTime(new Date());*/
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/addMytest")
	public @ResponseBody String addMytest(Mytest mytest) throws Exception {
		WebObj webObj = new WebObj();
		try {
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.mytest.setCreatePersonId(new String(loginObj.getPersonId()));
			this.mytest.setCreator(loginObj.getPersonName());
			this.mytest.setCreateTime(new Date());*/
			mytest.setId(PKCreator.getPk());
			mytestService.create(mytest);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add(e.getMessage());
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/viewMytest")
	public ModelAndView viewMytest(Mytest mytest,ModelAndView mv) throws Exception {
		mv.setViewName("/base/jsp/mytest/viewMytest.jsp");
		try {
			String id = mytest.getId();
			Mytest mytestVO = mytestService.findObjectById(id);
			mv.addObject("mytest",mytestVO);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/editMytest")
	public ModelAndView editMytest(String id) throws Exception {
		ModelAndView mv = new ModelAndView("/base/jsp/mytest/editMytest.jsp");
		try {
			Mytest mytest = mytestService.findObjectById(id);
			mv.addObject("mytest", mytest);
			WebObj webObj = new WebObj();
			mv.addObject("webObj",webObj);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/modMytest")
	public @ResponseBody String modMytest(String id,Mytest mytestVO) throws Exception {
		WebObj webObj = new WebObj();
		try {
			Mytest mytest = mytestService.findObjectById(id);
			mytest = (Mytest) MethodUtil.copyProperties(mytest, mytestVO);
			mytestService.update(mytest);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add(e.getMessage());
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/delMytest")
	public ModelAndView delMytest(String id,WebObj webObj) throws Exception {
		String[] ids = webObj.getSelectedRow();
		try {
			if(ids == null) { //删除单个
				if(id == null) {
					ids = new String[0];
				} else {
					ids = new String[]{String.valueOf(id)};
				}
			}
			String[] idsStr = new String[ids.length];
			int i = 0;
			for(String idVO: ids) {
				idsStr[i++] = new String(idVO);
			}
			mytestService.delete(idsStr);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return listMytest(webObj);
	}
}
